﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Expressing_Label
{
    public partial class ExperimentMiFrm : Form
    {
        private bool c_bMoving = false;
        private Point c_pStart = new Point();
        private Point[] pnt = new Point[8];
        private Bitmap c_bmpFormBackground = null;
        private bool m_TextFocus = false;
        private int c_tmpUserSum = 1;
        private int c_tmpUserId = 0;
        private DateTime c_tmpStartTime = PublicResources.g_dtStartTime;
        private string c_tmpWeather = "";
        private string c_tmpLocation = "";
        

        public ExperimentMiFrm()
        {
            InitializeComponent();
            InitializeFormBorder();
            InitializeTextBox();
           
        }

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreatePolygonRgn(ref Point lpPoint, int nCount, int nPolyFillMode);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bReDraw);

        private void InitializeFormBorder()
        {
            pnt[0] = new Point(40, 0);
            pnt[1] = new Point(Width, 0);
            pnt[2] = new Point(Width, Height);
            pnt[3] = new Point(40, Height);
            pnt[4] = new Point(40, ConfigurationFrm.g_yExperimentButton + 20);
            pnt[5] = new Point(20, ConfigurationFrm.g_yExperimentButton);
            pnt[6] = new Point(40, ConfigurationFrm.g_yExperimentButton - 20);
            pnt[7] = new Point(40, 0);

            IntPtr hRgn = CreatePolygonRgn(ref pnt[0], 16, 1);
            SetWindowRgn(Handle, hRgn, true);

            c_bmpFormBackground = new Bitmap(Width, Height);
            Graphics gra = Graphics.FromImage(c_bmpFormBackground);
            gra.Clear(Color.WhiteSmoke);
            Pen p = new Pen(Color.Purple, 4);
            for (int i = 0; i < 7; i++)
            {
                gra.DrawLine(p, pnt[i], pnt[i + 1]);
            }
            this.BackgroundImage = c_bmpFormBackground;
            gra.Dispose();
        }

        private void InitializeTextBox()
        {
            foreach (Control c in Controls)
            {
                if (c is TextBox)
                {
                    TextBox tb = (c as TextBox);
                    tb.Enter += new System.EventHandler(TextBox_Enter);
                    tb.MouseUp += new System.Windows.Forms.MouseEventHandler(TextBox_MouseUp);
                }
            }
        }

        private void btnFinished_Click(object sender, EventArgs e)
        {
            PublicResources.g_iConfiguraState = 5;
            PublicResources.g_iUserNum = c_tmpUserSum;
            PublicResources.g_iUserID = c_tmpUserId;
            PublicResources.g_dtStartTime = c_tmpStartTime;
            PublicResources.g_strWeather = c_tmpWeather;
            PublicResources.g_strLocation = c_tmpLocation;
            PublicResources.g_iExperimentID = Convert.ToInt32(tbExperimentID.Text.Trim());
            this.Owner.Owner.Owner.Show();
            this.Owner.Owner.Close();
        }


        private void ExperimentMiFrm_Load(object sender, EventArgs e)
        {
            this.Left = this.Owner.Location.X + this.Owner.Width - 40;
            this.Top = this.Owner.Location.Y;
            PublicResources.g_iConfiguraState = 4;
            tbExperimentID.Text = PublicResources.g_iExperimentID.ToString();
            txtNumOfTester.Text = PublicResources.g_iUserNum.ToString();
            txtID.Text = PublicResources.g_iUserID.ToString();
            txtStartTime.Text = PublicResources.g_dtStartTime.ToString();
            txtWeather.Text = PublicResources.g_strWeather;
            txtLocation.Text = PublicResources.g_strLocation;
        }

        private void ExperimentMiFrm_MouseDown(object sender, MouseEventArgs e)
        {
            c_bMoving = true;
            c_pStart = MousePosition;
        }

        private void ExperimentMiFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if (c_bMoving)
            {
                int x = MousePosition.X - c_pStart.X;
                int y = MousePosition.Y - c_pStart.Y;
                this.Left += x;
                this.Top += y;
                this.Owner.Left += x;
                this.Owner.Top += y;
                c_pStart = MousePosition;
            }
        }

        private void ExperimentMiFrm_MouseUp(object sender, MouseEventArgs e)
        {
            c_bMoving = false;
        }

        private void ExperimentMiFrm_MouseLeave(object sender, EventArgs e)
        {
            c_bMoving = false;
        }

        private void TextBox_Enter(object sender, EventArgs e)
        {
            if (sender is TextBox)
            {
                ((TextBox)sender).ForeColor = Color.Black;
                ((TextBox)sender).SelectAll();
                m_TextFocus = true;
            }
        }

        private void TextBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (sender is TextBox)
            {
                if (m_TextFocus)
                {
                    ((TextBox)sender).SelectAll();
                }
                m_TextFocus = false;
            }
        }

        private void txtNumOfTester_Leave(object sender, EventArgs e)
        {
            bool ok = true;
            int id = 1;
            try
            {
                id = Convert.ToInt32(txtID.Text.Trim());
            }
            catch (Exception ex)
            {
                if (MessageBox.Show("Invalid Input!") == DialogResult.OK)
                {
                    txtNumOfTester.SelectAll();
                    txtNumOfTester.Focus();
                }
                ok = false;
            }
            if (ok)
            {
                c_tmpUserSum = id;
            }
        }

        private void txtID_Leave(object sender, EventArgs e)
        {
            bool ok = true;
            int id = 0;
            try
            {
                id = Convert.ToInt32(txtID.Text.Trim());
            }
            catch (Exception ex)
            {
                if (MessageBox.Show("Invalid Input!") == DialogResult.OK)
                {
                    txtID.SelectAll();
                    txtID.Focus();
                }
                ok = false;
            }
            if (ok)
            {
                c_tmpUserId = id;
            }
        }

        private void txtStartTime_Leave(object sender, EventArgs e)
        {
            bool ok = true;
            DateTime dt = new DateTime();
            try
            {
                dt = DateTime.ParseExact(txtStartTime.Text.Trim(), "yyyy/MM/dd HH:mm:ss",System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (System.Exception ex)
            {
                if (MessageBox.Show("Invalid Input!") == DialogResult.OK)
                {
                    txtStartTime.SelectAll();
                    txtStartTime.Focus();
                }
                ok = false;
            }
            if (ok)
            {
                c_tmpStartTime = dt;
            }
        }

        private void txtWeather_Leave(object sender, EventArgs e)
        {
            //TODO:Input Valid Judge
            c_tmpWeather = txtWeather.Text.Trim();
        }

        private void txtLocation_Leave(object sender, EventArgs e)
        {
            c_tmpLocation = txtLocation.Text.Trim();
        }

    }
}
